import { get } from "../utils/Axios";

const getCurCity = () => {
  return new Promise((resolve, reject) => {
    const cacheCityInfo = JSON.parse(localStorage.getItem("cache_city_msg"));

    // 如果缓存中不存在城市信息 则获取当地信息
    if (!cacheCityInfo) {
      const curCity = new window.BMapGL.LocalCity();
      curCity.get(
        (result) => {
          get("/area/info", {
            name: result.name,
          }).then((data) => {
            result.value = data.data.body.value;
            result.label = data.data.body.label;
            localStorage.setItem("cache_city_msg", JSON.stringify(result));
            resolve(result);
          });
        },
        (err) => {
          reject(err);
        }
      );
    } else {
      resolve(cacheCityInfo);
    }
  });
};
export default getCurCity;
